package middleware

import (
	"fmt"
	"net/http"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/config"
	"gitee.com/liuxuezhan/ar-platform/base-common/middleware"
	"gitee.com/liuxuezhan/ar-platform/base-common/rpc"
	login "gitee.com/liuxuezhan/ar-platform/rpc-client/loginClient/client"
	"github.com/zeromicro/go-zero/core/logc"
)

func WhiteHeaderPath() map[string]int {
	return map[string]int{
		"/v1/bcpay/payment/notify":     1,
		"/v1/tbpay/payment/notify":     2,
		"/v1/payfun/payment/notify":    3,
		"/v1/sojupay/payment/notify":   4,
		"/v1/starpay/payment/notify":   5,
		"/v1/easypay/payment/notify":   6,
		"/v1/notify/test":              0,
		"/v1/qqhfpay/payment/notify":   7,
		"/v1/stanpayng/payment/notify": 8,
		"/v1/bpay/payment/notify":      9,
		"/v1/uzpay/payment/notify":     10,
		"/v1/paythere/payment/notify":  11,
		"/v1/bspay/payment/notify":     12,
		"/v1/bgw/payment/notify":       13,
	}
}

func CheckTokenHandle(c config.Config) middleware.CheckRequestTokenFunc {
	return func(r *http.Request, token string) int64 {
		rpcC, _ := rpc.NewClient(rpc.Config{
			Host:     c.LoginRpc,
			AppName:  c.Name,
			Business: c.Name,
		})
		ctx := r.Context()
		res, err := login.NewUser(rpcC).ParseToken(ctx, &login.ParseTokenRequest{
			Token: token,
		})
		if err != nil {
			logc.Error(ctx, fmt.Sprintf("请求错误 : "+r.Method+" 信息为 :", err))
		}

		return res.GetUserId()
	}
}
